package game.mapviewer.popups 
{
	import flash.display.Sprite;
	import game.mapviewer.MapViewer;
	import game.mapviewer.popups.Popup;
	/**
	 * ...
	 * @author rnk
	 */
	public class PopupsLayer extends Sprite
	{
		private var popups:Array = [];
		public var papa:MapViewer;
		
		public function PopupsLayer() 
		{
			
		}
		
		public function ShowPopupCentered(popup:Class, params:Object = null, single:Boolean = false):Popup
		{
			if (single) ClosePopups(popup);
			
			var somePopup:Popup = new popup();
			addChild(somePopup);
			somePopup.papa = this;
			somePopup.params = params;
			somePopup.Init();
			somePopup.x = (stage.width - somePopup.width)/2;
			somePopup.y = (stage.height - somePopup.height)/2;
			popups.push(somePopup);
			return somePopup;
			
		}
		
		public function ShowPopup(popup:Class, _x:Number, _y:Number, params:Object = null, single:Boolean = false):void
		{
			if (single) ClosePopups(popup);
			
			var somePopup:Popup = new popup();
			somePopup.x = _x;
			somePopup.y = _y;
			addChild(somePopup);
			somePopup.papa = this;
			somePopup.params = params;
			somePopup.Init();
			popups.push(somePopup);
		}
		
		public function ClosePopup(popup:Popup):void
		{
			var idx:int = popups.indexOf(popup);
			if (idx >= 0) popups.splice(idx, 1);
			popup.Die();
			removeChild(popup);
		}
		
		public function ClosePopups(popupClass:Class=null):void 
		{
			for (var i:int = popups.length - 1; i >= 0 ; i-- )
			{
				var popup:Popup = popups[i];
				if (popup is popupClass || popupClass == null)
				{
					popup.Die();
					removeChild(popup);
					popups.splice(i,1);
				}
				
			}
		}
		
	}

}